﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YZDD.Models;

namespace YZDD.Controllers
{
    public class Info {
        public static string answer;
        public static string question;
        public static int qID=1;
    }
    public class QuestionsController : Controller
    {
        private QuestionDBContext db = new QuestionDBContext();

        //
        // GET: /Questions/

        public ActionResult Game(string answer = null)
        {
            var questions = from m in db.Questions select m;
            if(answer == null){
                //Random rd = new Random();
                //int num = questions.Count();
                //int id = rd.Next();
                //id = id % num + 1;
                questions = questions.Where(s => s.ID.Equals(Info.qID));
                IQueryable<string> tempquestions = questions.Select(s => s.Quesiton);
                IQueryable<string> tempanswers = questions.Select(s => s.Answer);
                foreach (var productName in tempquestions)
                {
                    Info.question = productName;
                }
                foreach (var productName in tempanswers)
                {
                    Info.answer = productName;
                }
            }else if (Info.answer.Contains(answer))
            {
                Info.qID++;
                questions = questions.Where(s => s.ID.Equals(Info.qID));
                IQueryable<string> tempquestions = questions.Select(s => s.Quesiton);
                IQueryable<string> tempanswers = questions.Select(s => s.Answer);
                foreach (var productName in tempquestions)
                {
                    Info.question = productName;
                }
                foreach (var productName in tempanswers)
                {
                    Info.answer = productName;
                }
            }
            else
            {
                questions = questions.Where(s => s.ID.Equals(Info.qID));
            }
            return View(questions);
        }

        public string  Game2(string answer = null)
        {
            var questions = from m in db.Questions select m;
            if (answer == null)
            {
                //Random rd = new Random();
                //int num = questions.Count();
                //int id = rd.Next();
                //id = id % num + 1;
                questions = questions.Where(s => s.ID.Equals(Info.qID));
                IQueryable<string> tempquestions = questions.Select(s => s.Quesiton);
                IQueryable<string> tempanswers = questions.Select(s => s.Answer);
                foreach (var productName in tempquestions)
                {
                    Info.question = productName;
                }
                foreach (var productName in tempanswers)
                {
                    Info.answer = productName;
                }
            }
            else if (answer!="" && Info.answer.Contains(answer))
            {
                Info.qID++;
                questions = questions.Where(s => s.ID.Equals(Info.qID));
                IQueryable<string> tempquestions = questions.Select(s => s.Quesiton);
                IQueryable<string> tempanswers = questions.Select(s => s.Answer);
                foreach (var productName in tempquestions)
                {
                    Info.question = productName;
                }
                foreach (var productName in tempanswers)
                {
                    Info.answer = productName;
                }
            }
            else
            {
                questions = questions.Where(s => s.ID.Equals(Info.qID));
            }
            return Info.question;
        }

        public ActionResult Index()
        {
            return View(db.Questions.ToList());
        }

        //
        // GET: /Questions/Details/5

        public ActionResult Details(int id = 0)
        {
            Question question = db.Questions.Find(id);
            if (question == null)
            {
                return HttpNotFound();
            }
            return View(question);
        }

        //
        // GET: /Questions/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Questions/Create

        [HttpPost]
        public ActionResult Create(Question question)
        {
            if (ModelState.IsValid)
            {
                db.Questions.Add(question);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(question);
        }

        //
        // GET: /Questions/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Question question = db.Questions.Find(id);
            if (question == null)
            {
                return HttpNotFound();
            }
            return View(question);
        }

        //
        // POST: /Questions/Edit/5

        [HttpPost]
        public ActionResult Edit(Question question)
        {
            if (ModelState.IsValid)
            {
                db.Entry(question).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(question);
        }

        //
        // GET: /Questions/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Question question = db.Questions.Find(id);
            if (question == null)
            {
                return HttpNotFound();
            }
            return View(question);
        }

        //
        // POST: /Questions/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Question question = db.Questions.Find(id);
            db.Questions.Remove(question);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}